package com.sduept.nwld.dataserver.controller.equipment.assessment;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;

import org.primefaces.event.NodeSelectEvent;
import org.primefaces.model.TreeNode;

import com.sduept.bigdata.equipment.assessment.exception.ParameterException;
import com.sduept.bigdata.equipment.assessment.model.inspection.InspectionItem;
import com.sduept.bigdata.equipment.assessment.model.inspection.InspectionPlan;
import com.sduept.bigdata.equipment.assessment.model.inspection.InspectionStation;
import com.sduept.bigdata.equipment.assessment.service.InspectionExpertService;
import com.sduept.bigdata.equipment.assessment.service.InspectionItemService;
import com.sduept.bigdata.equipment.assessment.service.InspectionPlanService;
import com.sduept.bigdata.equipment.assessment.service.InspectionStationService;
import com.sduept.bigdata.web.jsf.controller.AbstractController;
import com.sduept.nwld.dataserver.util.LibTreeNode;
import com.sduept.permission.entity.DepartmentEntityBean;
import com.sduept.permission.manager.DepartManager;
/**
 * 
 * @author pengz
 *
 */
@Named
@ViewScoped
public class InspectionPlanDetailController extends AbstractController{

	private static final long serialVersionUID = -1313640077800385854L;
	@Inject
	private InspectionPlanService planService;
	@Inject
	private InspectionStationService instationService;
	@Inject
	private InspectionItemService itemService;
	@Inject
	private InspectionExpertService expertService;
//	@Inject
//	private SubstationLedgerManager stationLedgerM;
	@Inject
	private DepartManager dm;
	/**
	 * 当前评价计划
	 */
	private InspectionPlan currentInspectionPlan;
	/**
	 * 评价厂站树的根节点
	 */
	private TreeNode inspectionStationRoot;
	/**
	 * 选中的节点
	 */
	private TreeNode selectedNode;
	/**
	 * 选中的评价厂站
	 */
	private InspectionStation selectedInspectionStation;
	/**
	 * 此次评价厂站总数量
	 */
	private int inspectionStationTotalCount;
	/**
	 * 已完成厂站数量
	 */
	private int finishedCount;
	/**
	 * 未完成数量
	 */
	private int unFinishedCount;
	/**
	 * 该评价计划已扣分
	 */
	private float totalDeduct = 0.0f;
	//评价变电站数据展示table模型集合
	private List<InspStationTableModel> selectLists = new ArrayList<>();
	
	private Set<InspectionStation> stationAll = new HashSet<InspectionStation>();
	
	private String inspectionItemStatusColor;
	private String inspectionStationStatusColor;
	private int totalInspectionItemSize;
	private int finishedInspectionItemSize;
	private String affiliatedUnitName;
	private String assessmentUnitName;
	@PostConstruct
	public void init() {
		String planId = getParameter("id");
		currentInspectionPlan = planService.findById(planId);
		initInspectionStationRoot();
		initSelectedInspectionStationInfo();
		initTotalDeduct();
	}
	/**
	 * 初始化评价厂站树
	 */
	private void initInspectionStationRoot() {
		inspectionStationRoot = new LibTreeNode("root", null);
		Map<String, TreeNode> departNodeMap = new HashMap<String,TreeNode>();
		try {
			stationAll = instationService.findByInspectionPlan(currentInspectionPlan.getId());
			initCount(stationAll);
		} catch (ParameterException e) {
			e.printStackTrace();
		}
		int index=0;
		for (InspectionStation inspectionStation : stationAll) {
			index++;
			DepartmentEntityBean dept = null;
			if(inspectionStation.getInspectionScope()!=null) {
				dept = dm.findDepartmentById(inspectionStation.getInspectionScope());
			}
			if(dept!=null) {
				if(!departNodeMap.containsKey(dept.getId())) {
					LibTreeNode departNode = new LibTreeNode("department", dept, inspectionStationRoot);
					departNodeMap.put(dept.getId(), departNode);
					departNode.setExpanded(true);
				}			
				LibTreeNode stationNode = new LibTreeNode("inspectionStation", inspectionStation, departNodeMap.get(dept.getId()));
				if(index==1) {
					stationNode.setSelected(true);
					selectedNode = stationNode;
					selectedInspectionStation = inspectionStation;
				}
			}
		}
	}
	/**
	 * 
	 * @param currentStations
	 */
	private void initCount(Set<InspectionStation> currentStations) {
		inspectionStationTotalCount = currentStations.size();
		int num = 0;
		for (InspectionStation instation : currentStations) {
			if(instation.getStatus()==2) {
				num++;
			}
		}
		finishedCount = num;
		unFinishedCount = inspectionStationTotalCount-finishedCount;
	}
	/**
	 * 初始化已扣分项
	 */
	private void initTotalDeduct() {
		if(currentInspectionPlan.getStatus()==2||currentInspectionPlan.getStatus()==3) {
			for(InspectionStation instation : stationAll) {
				if(instation.getDeduct()!=null) {
					totalDeduct+=instation.getDeduct();
				}
			}
		}
	}
	/**
	 * 计算逾期评价厂站数量
	 * @return
	 */
	public int getOvertimeCount() {
		int count = 0;
		Date now = new Date();
		for(InspectionStation station : stationAll) {
			if(station.getStatus()!=2&&station.getEndTime()!=null) {
				if(station.getEndTime().before(now)) {
					count++;
				}
			}
		}
		return count;
	}
	/**
	 * 计算剩余天数
	 * @return
	 */
	public int getRemainingDays() {
		Date now = new Date();
		if(currentInspectionPlan.getEndTime()!=null) {
			int day = (int) ((currentInspectionPlan.getEndTime().getTime() - now.getTime()) / 1000 / 60 / 60 / 24);
			return day;
		}
			
		return 0;
	}
	/**
	 * 处理节点选中事件
	 * @param event
	 */
	public void onNodeSelect(NodeSelectEvent event) {
		TreeNode node = event.getTreeNode();
		if("inspectionStation".equals(node.getType())) {
			selectedInspectionStation = (InspectionStation) node.getData();
			initSelectedInspectionStationInfo();
		}
	}
	/**
	 * 初始化选中变电站详细信息，展示在页面右侧
	 * @param selectedInspectionStation2 
	 */
	private void initSelectedInspectionStationInfo() {
		try {
			if(selectedInspectionStation!=null) {
				List<InspectionItem> inspectionCriterions = itemService.findCreterionWithQuestionEquipmentsBySubId(selectedInspectionStation.getId());
				if(inspectionCriterions!=null) {
					selectLists = InspStationTableModel.getListByInspectionCriterions(inspectionCriterions);
					InspStationTableModel.sortModelList(selectLists);
					totalInspectionItemSize = inspectionCriterions.size();
					finishedInspectionItemSize = 0;
					for(InspectionItem item : inspectionCriterions) {
						if(item.getStatus()==1) {
							finishedInspectionItemSize++;
						}
					}
					if(selectedInspectionStation.getInspectionScope()!=null) {
						DepartmentEntityBean affiliatedUnit = dm.findDepartmentById(selectedInspectionStation.getInspectionScope());
						if(affiliatedUnit!=null) {
							affiliatedUnitName = affiliatedUnit.getName();
						}
					}
					if(selectedInspectionStation.getInspectionScope()!=null) {
						DepartmentEntityBean assessmentUnit = dm.findDepartmentById(selectedInspectionStation.getInspectionScope());
						if(assessmentUnit!=null) {
							assessmentUnitName = assessmentUnit.getName();
						}
					}
				}
			}
			
		} catch (ParameterException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 获得选中评价厂站的状态
	 * @return
	 */
	public String getInspectionStationStatusStr() {
		String statusStr = "";
		if(selectedInspectionStation!=null)
			switch (selectedInspectionStation.getStatus()) {
			case 0:
				statusStr="未开始";
				inspectionStationStatusColor = "blue";
				break;
			case 1:
				statusStr="进行中";
				inspectionStationStatusColor = "red";
				break;
			case 2:
				statusStr="已完成";
				inspectionStationStatusColor = "green";
				break;
			default:
				break;
			}
		return statusStr;
	}
	/**
	 * 获得选中的评价厂站已扣分量
	 * @return
	 */
	public String getInspectionStationDeduct() {
		Float deduct = 0.0f;
		if(selectedInspectionStation.getDeduct()!=null) {
			return selectedInspectionStation.getDeduct().toString();
		}
		return deduct.toString();
	}
	/**
	 * 获得评价专家集合字符串
	 * @return
	 */
	public String getInspectionExpertsStr() {
		StringBuffer result = new StringBuffer();
//		try {
//			if(selectedInspectionStation!=null) {
//				Set<InspectionExpert> experts = expertService.findByInspectionStation(selectedInspectionStation.getId());
//				for (InspectionExpert expert : experts) {
//					result.append(expert.getName()).append(" ");
//				}
//			}
//		} catch (ParameterException e) {
//			log.info("专家获取失败！");
//		}
		return result.toString();
	}
	/**
	 * 获取不同状态对应的展示内容
	 * @param status
	 * @return
	 */
	public String getStatusStr() {
		String statusStr = "";
		switch (currentInspectionPlan.getStatus()) {
		case 0:
			statusStr="草稿";
			break;
		case 1:
			statusStr="已发布";
			break;
		case 2:
			statusStr="进行中";
			break;
		case 3:
			statusStr="已完成";
			break;
		default:
			break;
		}
		return statusStr;
	}
	public String getStatusColor() {
		String statusColor = "";
		switch (currentInspectionPlan.getStatus()) {
		case 0:
			statusColor = "#3c8dbc";
			break;
		case 1:
			statusColor = "#3c8dbc";
			break;
		case 2:
			statusColor = "#dd4b39";
			break;
		case 3:
			statusColor = "#00a65ac7";
			break;
		default:
			break;
		}
		return statusColor;
	}
	public String getInspectionItemStatusStr(int status) {
		String statusStr = "";
		switch (status) {
		case 0:
			statusStr="未完成";
			inspectionItemStatusColor = "red";
			break;
		case 1:
			statusStr="已完成";
			inspectionItemStatusColor = "green";
			break;
		}
		return statusStr;
	}
	public InspectionPlan getCurrentInspectionPlan() {
		return currentInspectionPlan;
	}
	public void setCurrentInspectionPlan(InspectionPlan currentInspectionPlan) {
		this.currentInspectionPlan = currentInspectionPlan;
	}
	public TreeNode getInspectionStationRoot() {
		return inspectionStationRoot;
	}
	public void setInspectionStationRoot(TreeNode inspectionStationRoot) {
		this.inspectionStationRoot = inspectionStationRoot;
	}
	public TreeNode getSelectedNode() {
		return selectedNode;
	}
	public void setSelectedNode(TreeNode selectedNode) {
		this.selectedNode = selectedNode;
	}
	public InspectionStation getSelectedInspectionStation() {
		return selectedInspectionStation;
	}
	public void setSelectedInspectionStation(InspectionStation selectedInspectionStation) {
		this.selectedInspectionStation = selectedInspectionStation;
	}
	public int getInspectionStationTotalCount() {
		return inspectionStationTotalCount;
	}
	public void setInspectionStationTotalCount(int inspectionStationTotalCount) {
		this.inspectionStationTotalCount = inspectionStationTotalCount;
	}
	public int getFinishedCount() {
		return finishedCount;
	}
	public void setFinishedCount(int finishedCount) {
		this.finishedCount = finishedCount;
	}
	public int getUnFinishedCount() {
		return unFinishedCount;
	}
	public void setUnFinishedCount(int unFinishedCount) {
		this.unFinishedCount = unFinishedCount;
	}
	public List<InspStationTableModel> getSelectLists() {
		return selectLists;
	}
	public void setSelectLists(List<InspStationTableModel> selectLists) {
		this.selectLists = selectLists;
	}
	public float getTotalDeduct() {
		return totalDeduct;
	}
	public void setTotalDeduct(float totalDeduct) {
		this.totalDeduct = totalDeduct;
	}
	public String getInspectionItemStatusColor() {
		return inspectionItemStatusColor;
	}
	public void setInspectionItemStatusColor(String inspectionItemStatusColor) {
		this.inspectionItemStatusColor = inspectionItemStatusColor;
	}
	public String getInspectionStationStatusColor() {
		return inspectionStationStatusColor;
	}
	public void setInspectionStationStatusColor(String inspectionStationStatusColor) {
		this.inspectionStationStatusColor = inspectionStationStatusColor;
	}
	public int getTotalInspectionItemSize() {
		return totalInspectionItemSize;
	}
	public void setTotalInspectionItemSize(int totalInspectionItemSize) {
		this.totalInspectionItemSize = totalInspectionItemSize;
	}
	public int getFinishedInspectionItemSize() {
		return finishedInspectionItemSize;
	}
	public void setFinishedInspectionItemSize(int finishedInspectionItemSize) {
		this.finishedInspectionItemSize = finishedInspectionItemSize;
	}
	public String getAffiliatedUnitName() {
		return affiliatedUnitName;
	}
	public void setAffiliatedUnitName(String affiliatedUnitName) {
		this.affiliatedUnitName = affiliatedUnitName;
	}
	public String getAssessmentUnitName() {
		return assessmentUnitName;
	}
	public void setAssessmentUnitName(String assessmentUnitName) {
		this.assessmentUnitName = assessmentUnitName;
	}
	
}
